var  $inputLogin, 
	 $inputSenha, 
	 $inputCaptchaChallenge,
	 $inputCaptcha,
	 $logonErrorMsg,
	 $captchaErrorMsg;

$(function(){
	//setTimeout(1000);
	$inputLogin				=	$("input#usuario"),
	$inputSenha				=	$("input#senha"),
	$inputCaptcha			=	$("input#captcha"), 
	existeSenha				=	$inputSenha.length;
	existeCaptcha			=	$inputCaptcha.length;
	var 
	$logonErrorMsg			=	"<br>Dados de logon inválidos !",
	$captchaErrorMsg		=   "<br>Captcha incorreto !",
	$valLogon				= 	$("form.logon_form").validate({
		rules:{
            usuario:{
                // campoNome será obrigatorio (required) e terá tamanho minimo (minLength)
                required: true, minlength: 7, maxlength : 7
            },
            senha:{
                // campoMensagem será obrigatorio (required) e terá tamanho minimo (minLength)
                required: true, minlength: 2, maxlength : 20
            },
            captcha:{
                // campoMensagem será obrigatorio (required) e terá tamanho minimo (minLength)
                required: true, minlength: 4, maxlength : 4
            }
        },
        // Define as mensagens de erro para cada regra
        messages:{
            usuario:{
                required	: "<br>Digite sua matricula",
                minlength   : "<br>Digite no m&iacute;nimo, 7 (sete) caracteres",
                maxlength   : "<br>Digite no m&aacute;ximo, 7 (sete) caracteres"
            },
            senha:{
                required	: "<br>Digite a sua senha",
                minlength   : "<br>Digite no m&iacute;nimo, 2 (dois) caracteres",
                maxlength   : "<br>Digite no m&aacute;ximo, 20 (vinte) caracteres"
            },
            captcha:{
                required	: "<br>Digite o captcha acima",
                minlength   : "<br>Digite no m&iacute;nimo, 4 (quatro) caracteres",
                maxlength   : "<br>Digite no m&aacute;ximo, 4 (quatro) caracteres"
            }
        },
        errorPlacement: function( error, element ) {
			error.html() == $logonErrorMsg ? $(".logon-credentials").after(error) : error.insertAfter(element);
		},
		submitHandler: function(form) {
			$.ajax({
				type : 'POST', 
				url  : 'ajax/auth.asp?acao=logon',
				data : {usuario : $inputLogin.attr("value"), senha: $inputSenha.attr("value")},
				success : function(data){
					if (data == "true"){
						$.cookie("informativos_logon", $inputLogin.attr("value"), { path: "/" });
						window.location.href='home.asp';
					}
				}
			});
		}
	});
	
	if (existeSenha)
		$inputSenha.rules("add", {
			remote: {
				url: "ajax/auth.asp?acao=logon",
				data: {
					usuario: function() {
						return $inputLogin.attr("value");
					}
				},
				error: function( request, status, error ) {
					$valLogon.showErrors({"senha": $logonErrorMsg});
				}
			},
			maxlength: 36,
			messages: {
				remote: $logonErrorMsg
			}
		});

	if (existeCaptcha) {
		ReloadCaptcha();
		$inputCaptcha.rules("add", {
			minlength: 4,
			remote: {
				url: "ajax/captcha.asp?acao=check",
				data: {
					captcha: function() {
						return $inputCaptcha.attr("value");
					}
				},
				error: function( request, status, error ) {
					$valLogon.showErrors({"captcha": $captchaErrorMsg});
				}
			},
			messages: {
				remote: $captchaErrorMsg
			}
		});
	} else {
		EFocus();	
	}

});

function ReloadCaptcha() {
	$inputCaptcha.attr("value", "");
	$("img#captcha_image").attr("src", "ajax/captcha.asp?acao=getImage\u0026timestamp="+new Date().getTime());
	EFocus();
}

function EFocus() {
	if ($.cookie("informativos_logon") != ""){
		$inputLogin.attr("value",$.cookie("informativos_logon"));
	}
	if ($inputLogin.attr("value") == "") $inputLogin.focus();
	else if ($inputSenha.attr("value") == "" && existeSenha) $inputSenha.focus();
	else if ($inputCaptcha.attr("value") == "" && existeCaptcha) $inputCaptcha.focus();
}